package com.huiyou.adminapi.controller;


import com.huiyou.adminapi.common.model.Dto.SysUserDto;
import com.huiyou.adminapi.common.model.SysRole;
import com.huiyou.adminapi.common.model.SysUser;
import com.huiyou.adminapi.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/sysUser")
public class UserController {

    @Autowired
    UserService userService;

    @GetMapping
    public Page<SysUser> getUserList(@PageableDefault(size = 10, sort = "modifyTime", direction = Sort.Direction.DESC) Pageable pageable, @RequestParam String name) {
        return userService.findByNameContaining(name, pageable);
    }

    @GetMapping("/{id}")
    public SysUser getUserById(@PathVariable String id) {
        return userService.findById(id);
    }

    @PostMapping
    public void save(@RequestBody SysUser sysUser) {
        userService.save(sysUser);
    }

    @PutMapping("/{id}")
    public void edit(@RequestBody SysUser sysUser) {
        userService.edit(sysUser);
    }

    @GetMapping("/bindRole")
    public List<SysRole> bindRole(@RequestParam String userId) {
        return userService.getRoleBindList(userId);
    }

    @PostMapping("/saveRole")
    public void saveRole(@RequestBody List<SysRole> roleList) {
        userService.saveRole(roleList);
    }

    @DeleteMapping("/deleteRole")
    public void deleteRole(@RequestParam String userId) {
        userService.deleteRole(userId);
    }

    @PostMapping("/login")
    public SysUser login(@RequestBody SysUserDto sysUserDto) {
        return userService.userLogin(sysUserDto.getAccount(), sysUserDto.getPassword());
    }
}
